package org.jasper.impl.base;

import org.jasper.api.base.KieBaseConfiguration;
import org.jasper.api.base.KnowledgeBaseFactoryService;
import org.jasper.api.knowledge.KnowledgeBase;
import org.jasper.api.session.KieSessionConfiguration;
import org.jasper.impl.service.ServiceRegistryImpl;

import java.util.Properties;

/**
 * Created by OP038074 on 2018/8/3.
 */
public class KnowledgeBaseFactory {

    private static KnowledgeBaseFactoryService factoryService;

    public static KieSessionConfiguration newKnowledgeSessionConfiguration() {
        return getKnowledgeBaseFactoryService().newKnowledgeSessionConfiguration();
    }

    public static synchronized void setKnowledgeBaseServiceFactory(KnowledgeBaseFactoryService serviceFactory) {
        KnowledgeBaseFactory.factoryService = serviceFactory;
    }

    private static synchronized KnowledgeBaseFactoryService getKnowledgeBaseFactoryService() {
        if ( factoryService == null ) {
            loadServiceFactory();
        }
        return factoryService;
    }

    private static void loadServiceFactory() {
        setKnowledgeBaseServiceFactory( ServiceRegistryImpl.getInstance().get( KnowledgeBaseFactoryService.class ) );
    }

    public static KnowledgeBase newKnowledgeBase(String kbaseId,
                                                 KieBaseConfiguration conf) {
        return getKnowledgeBaseFactoryService().newKnowledgeBase( kbaseId, conf );
    }

    public static KieBaseConfiguration newKnowledgeBaseConfiguration(Properties properties,
                                                                     ClassLoader... classLoaders) {
        return getKnowledgeBaseFactoryService().newKnowledgeBaseConfiguration( properties,
                classLoaders );
    }
}
